package com.tgpms.vforms.controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.QueryException;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "ContractController", tags = "D4001 合同信息")
@RestController
@RequestMapping(value = "/ev/vform/contract")
public class ContractController {
	
	@Resource
    private VformInterface vformInterface;

	@ApiOperation(value = "查询合同信息", notes = "查询合同信息", httpMethod = "POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "pageNo",value = "页数",required = true,defaultValue = "1"),
    	@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,defaultValue = "10"),
    	@ApiImplicitParam(name = "dataowner",value = "数据拥有者",required = false),
    	@ApiImplicitParam(name = "division",value = "机构/责任单位/内部单位",required = false),
    	@ApiImplicitParam(name = "company",value = "承包公司编码",required = false),
    	@ApiImplicitParam(name = "poNo",value = "合同编码",required = false),
    	@ApiImplicitParam(name = "description",value = "合同描述",required = false),
    	@ApiImplicitParam(name = "invoice",value = "是否带支付清单项",required = false,defaultValue = "1")
    })
    @PostMapping(value = "/selContractList")
    public Result<Object> selContractList(
    		@RequestParam(value = "pageNo", required = true,defaultValue = "1") String pageNo,
    		@RequestParam(value = "pageSize", required = true,defaultValue = "10") String pageSize,
    		@RequestParam(value = "dataowner", required = false) String dataowner,
    		@RequestParam(value = "division", required = false) String division,
    		@RequestParam(value = "company", required = false) String company,
    		@RequestParam(value = "poNo", required = false) String poNo,
    		@RequestParam(value = "description", required = false) String description,
    		@RequestParam(value = "invoice", required = false,defaultValue = "1") String invoice) {
        Result<Object> result = new Result<Object>();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
        	JSONObject json = new JSONObject();
        	if(invoice.equals("0")) {
        		json.put("[SQL1]", "");
        		json.put("[SQL2]", "");
        	}else {
        		json.put("[SQL1]", " ,iov.invoice_no as invoice_no,iov.ap_ref as ap_ref ");
        		json.put("[SQL2]", " left join INVOICE iov on iov.po_no = a.po_no ");
        	}
        	String condition = "";
        	if(!Common.isEmpty(company)) {
        		condition=condition+" and a.company like '%"+company+"%'";
        	}
        	if(!Common.isEmpty(division)) {
        		condition=condition+" and a.division = '"+division+"'";
        	}
        	if(!Common.isEmpty(poNo)) {
        		condition=condition+" and a.po_no like '%"+poNo+"%'";
        	}
        	if(!Common.isEmpty(description)) {
        		condition=condition+" and a.description like '%"+description+"%'";
        	}
        	json.put("[SQL3]", condition);
        	if(!Common.isEmpty(dataowner)) {
        		result = vformInterface.multiForMsQueryBySql("SHTDL001", json.toJSONString(), pageNo, pageSize,"Y","Y",dataowner);
        	}else {
        		result = vformInterface.multiForMsQueryBySql("SHTDL001", json.toJSONString(), pageNo, pageSize,"Y",null,null);
        	}
           
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        return result;
    }
}
